package com.yehuishou.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.security.auth.login.FailedLoginException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.dao.PartsDao;
import com.yehuishou.pojo.AssemblyPartsList;
import com.yehuishou.pojo.BackOrder;
import com.yehuishou.pojo.Parts;
import com.yehuishou.pojo.User;
import com.yehuishou.pojo.Version;
import com.yehuishou.service.BackOrderService;
import com.yehuishou.service.PartsService;
/**
 * 
 * @author cm
 *
 */
@Controller
public class PartsController {
	
	@Autowired
	private PartsService partsService;
	
	@Resource
	private BackOrderService backOrderService;
	@RequestMapping("/showParts")
       public  String show(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
   			@RequestParam(required = true, defaultValue = "6") Integer pageSize,
    		   HttpServletRequest request,AssemblyPartsList assemblyPartsList ){
		PageInfo<AssemblyPartsList> pageinfo=partsService.getList(pageNum, pageSize, assemblyPartsList);
		request.setAttribute("pageinfo", pageinfo);
		request.setAttribute("assemblyPartsList", assemblyPartsList);
    	   return  "content-3_3";   
    	   
       }
	@RequestMapping("/showStore")
	 public  String showStores(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
	   			@RequestParam(required = true, defaultValue = "10") Integer pageSize,
	    		   HttpServletRequest request,BackOrder backOrder){
	
			PageInfo<BackOrder> pageinfo=backOrderService.selectStore(pageNum, pageSize, backOrder);
			
		
			request.setAttribute("pageinfo", pageinfo);
			request.setAttribute("backOrder", backOrder);
			
			
			
	    	   return  "content-3_4";   
	    	   
	       }
	@RequestMapping("/deleteStore")
	@ResponseBody
	public boolean deleteStore(Integer sid){
		boolean b=partsService.deleteStore(sid);
		if (b) {
			return true;
		} else {
			return false;
		}
		
    }
	@RequestMapping("/delete")
	@ResponseBody
	public boolean delete(Integer pid){
		boolean b=partsService.delete(pid);
		if (b) {
			return true;
		} else {
			return false;
		}
		
    }
	@RequestMapping("/add")
	public String add(){
		return "content-3_3.1";
	}
	
	
	@RequestMapping("/saveAdd")
	public String saveadd(AssemblyPartsList assemblyPartsList,HttpServletRequest request){
	
	partsService.insertPart(assemblyPartsList);
		return "redirect:showParts";
	}
	
	@RequestMapping("/checkPhone")
	@ResponseBody
	public Map<String, Object> checkPhone(String phone,HttpServletRequest request){
		Map<String, Object> map = new HashMap<>();
		boolean flag = true;
		System.out.println(phone);
		String string=request.getParameter("phone");
		List<String> users=partsService.getPhone();
		List<Parts> list=partsService.getUid(phone);
		if (users.contains(string)) {
		flag=true;
	    }else{
		flag=false;
		}
		map.put("flag", flag);
		
		if(list.size()>0){
		
		map.put("uid", list.get(0).getUid());
		}
		
		return map;
	}
	
	@RequestMapping("/checkXid")
	@ResponseBody
	public  Map<String, Object> select(Integer xid){
		List<Version> b=partsService.getProcessor_version(xid);
		
		System.out.println("WWWWWWW:"+b);
		Map<String, Object> map = new HashMap<>();
		map.put("b", b);
		return map;
		
		
    }
	
	@RequestMapping("/editPrices")
	public String editPrices(HttpServletRequest request,String aid) {
      System.out.println("aasdfafafa:"+aid);
      request.setAttribute("aid", aid);
	return "editPrices";
	}
	@RequestMapping("/newPricees")
	@ResponseBody
	public Boolean newPricees(Parts parts,HttpServletRequest request) {
		
	partsService.updatePrice1(parts);
	return  true;
	}
	@RequestMapping("/deleteParts")
	@ResponseBody
	public Boolean newPr(AssemblyPartsList assemblyPartsList,HttpServletRequest request) {
		
	   return partsService.deletesPart(assemblyPartsList);
	
	}
	
	@RequestMapping("/deletePar")
	@ResponseBody
	public Boolean deletePar(@RequestParam(value = "gids[]") Integer[] gids) {
		
	   partsService.deleteP(gids);
	   return true;
	
	}
	
	
}
